package com.jjx.trainingapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.jjx.trainingapi.common.RT;
import com.jjx.trainingapi.util.QiNiuOssUploadBean;
import com.jjx.trainingapi.util.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LoveHuahua
 * @date 2022年06月03日 23:40
 * @description believe in yourself
 */
@RestController
@RequestMapping("/file")
@SaCheckLogin
public class FilesController {

    @Autowired
    QiNiuUtils qiNiuUtils;

    /**
     * 上传头像
     *
     * @return
     */
    @PostMapping("/uploadImg")
    public Object uploadImg(MultipartFile file, String path) {
        return RT.ok(qiNiuUtils.uploadMultipartFile(file, path + System.currentTimeMillis() + "-" + file.getOriginalFilename()));

    }


    /**
     * 批量上传图片
     *
     * @return
     */
    @PostMapping("/batchUploadImg")
    public Object batchUploadImg(MultipartFile[] files, String path) {
        List<QiNiuOssUploadBean> res = new ArrayList<>();
        Arrays.stream(files).forEach(file -> {
            QiNiuOssUploadBean qiNiuOssUploadBean = qiNiuUtils.uploadMultipartFile(file, path + System.currentTimeMillis() + "-" + file.getOriginalFilename());
            res.add(qiNiuOssUploadBean);
        });

        return RT.ok(res);
    }
}
